home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / source.exe / POSIX / GREP / REGSUB.C < prev    next >
C/C++ Source or Header  |  1992-09-30  |  2KB  |  91 lines

  1. /*
  2.  * regsub
  3.  *
  4.  *    Copyright (c) 1986 by University of Toronto.
  5.  *    Written by Henry Spencer.  Not derived from licensed software.
  6.  *
  7.  *    Permission is granted to anyone to use this software for any
  8.  *    purpose on any computer system, and to redistribute it freely,
  9.  *    subject to the following restrictions:
  10.  *
  11.  *    1. The author is not responsible for the consequences of use of
  12.  *        this software, no matter how awful, even if they arise
  13.  *        from defects in it.
  14.  *
  15.  *    2. The origin of this software must not be misrepresented, either
  16.  *        by explicit claim or by omission.
  17.  *
  18.  *    3. Altered versions must be plainly marked as such, and must not
  19.  *        be misrepresented as being the original software.
  20.  *
  21.  *    This version has alterations for ISO C usage. These changes are
  22.  *    Copyright 1992 DataFocus Incorporated. All rights reserved.
  23.  */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "regexp.h"
  27. #include "regmagic.h"
  28.  
  29. #ifndef CHARBITS
  30. #define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  31. #else
  32. #define    UCHARAT(p)    ((int)*(p)&CHARBITS)
  33. #endif
  34.  
  35. /*
  36.  - regsub - perform substitutions after a regexp match
  37.  */
  38. void
  39. #if __STDC__
  40. regsub (regexp *prog, char *source, char *dest)
  41. #else
  42. regsub(prog, source, dest)
  43. regexp *prog;
  44. char *source;
  45. char *dest;
  46. #endif
  47. {
  48.     register char *src;
  49.     register char *dst;
  50.     register char c;
  51.     register int no;
  52.     register int len;
  53. #if !__STDC__
  54.     extern char *strncpy();
  55. #endif
  56.  
  57.     if (prog == NULL || source == NULL || dest == NULL) {
  58.         regerror("NULL parm to regsub");
  59.         return;
  60.     }
  61.     if (UCHARAT(prog->program) != MAGIC) {
  62.         regerror("damaged regexp fed to regsub");
  63.         return;
  64.     }
  65.  
  66.     src = source;
  67.     dst = dest;
  68.     while ((c = *src++) != '\0') {
  69.         if (c == '&')
  70.             no = 0;
  71.         else if (c == '\\' && '0' <= *src && *src <= '9')
  72.             no = *src++ - '0';
  73.         else
  74.             no = -1;
  75.          if (no < 0) {    /* Ordinary character. */
  76.              if (c == '\\' && (*src == '\\' || *src == '&'))
  77.                  c = *src++;
  78.              *dst++ = c;
  79.          } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
  80.             len = prog->endp[no] - prog->startp[no];
  81.             (void) strncpy(dst, prog->startp[no], len);
  82.             dst += len;
  83.             if (len != 0 && *(dst-1) == '\0') {    /* strncpy hit NUL. */
  84.                 regerror("damaged match string");
  85.                 return;
  86.             }
  87.         }
  88.     }
  89.     *dst++ = '\0';
  90. }
  91.